//
//  WBStatusViewModel.swift
//  CFZWeiBo
//
//  Created by Cedric Zheng on 30/11/2016.
//  Copyright © 2016 Cedric Zheng. All rights reserved.
//

import UIKit

class WBStatusViewModel: NSObject {
    
    fileprivate var statusModel: WBStatusModel
    /// 发布人
    fileprivate var user: WBUser? {return statusModel.user}
    /// 转发微博
    fileprivate var retweeted_status: WBStatusModel? {return statusModel.retweeted_status}

    
    var screenName: String? {return user?.screen_name}
    
    var userIconURLStr: String? {return user?.avatar_large}
    /// 微博的正文
    var text: String?
    /// 图片的模型数组
    var pic_urls: [WBPictureModel]? {return statusModel.pic_urls}
    
    var vipImage: UIImage?
    
    var levelImage: UIImage?
    /// 微博的来源
    var sourceString: String?
    /// 创建时间
    var timeString: String?
    
    var retweetedStatusViewModel: WBStatusViewModel? {
        guard let retweeted_status = statusModel.retweeted_status else {
            return nil
        }
        return WBStatusViewModel(retweeted_status)
    }
    
    
    
    init(_ statusModel: WBStatusModel) {
        self.statusModel = statusModel
        
        super.init()
        
        processStatuses()
    }
    
    fileprivate func processStatuses() {
        
        if let timeStr = statusModel.created_at {
            timeString = timeStr.statusTimeStringToTimeCampareString(withDateFormatString: WeiboDateformatString, locale: locale)
        }
        
        if let rawSourceString = statusModel.source {
            sourceString = "来自 " + rawSourceString.subString(fromEndOf: "\">", toBeginOf: "</a>")
        }
        
        if let textString = statusModel.text {
            text = textString
        }
        
        processUser()
        
//        processRetweetedStatus()
    }
    
    fileprivate func processUser() {
        if let mbrank = user?.mbrank, mbrank > 0 && mbrank < 7 {
            levelImage = UIImage(named: "common_icon_membership_level\(mbrank)")
        }
        
        if let verified_type = user?.verified_type {
            switch verified_type {
            case 0:
                vipImage = UIImage(named: "avatar_vip")
            case 2, 3, 5:
                vipImage = UIImage(named: "avatar_enterprise_vip")
            case 220:
                vipImage = UIImage(named: "avatar_grassroot")
            default:
                vipImage = nil
            }
        }
    }
    
//    fileprivate func processRetweetedStatus() {
//        guard let retweetedText = statusModel.retweeted_status?.text else {
//            return
//        }
//        
//        let userName = statusModel.retweeted_status?.user?.screen_name ?? ""
//        statusModel.retweeted_status?.text = "@\(userName)：\(retweetedText)"
//    }
    
}
